using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using ConfigManager.Core.Instructions;
using NUnit.Framework;

namespace ConfigManager.Tests.Instructions
{
    [TestFixture()]
    public class SetValueInstructionTests
    {
        [Test()]
        public void SetInnerText()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"instructions\SetValueInstruction1.xml");

            XmlNode node = xmlDoc.SelectNodes("//comment()")[0];
            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("xpath", "/root//testNode[@name='x']");
            parameters.Add("value", "updated");

            SetValueInstruction target = new SetValueInstruction(xmlDoc, node, parameters);
            target.Run();

            Assert.AreEqual("updated", xmlDoc.SelectSingleNode("/root//testNode[@name='x']").InnerText, "Value was not updated.");
            Assert.AreEqual("y value", xmlDoc.SelectSingleNode("/root//testNode[@name='y']").InnerText, "Other values where mistakenly updated.");
        }

        [Test()]
        public void SetAttribute()
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"instructions\SetValueInstruction2.xml");

            XmlNode node = xmlDoc.SelectNodes("//comment()")[0];
            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("xpath", "/root//testNode[@name='x']/@attValue");
            parameters.Add("value", "updated");

            SetValueInstruction target = new SetValueInstruction(xmlDoc, node, parameters);
            target.Run();

            Assert.AreEqual("updated", xmlDoc.SelectSingleNode("/root//testNode[@name='x']/@attValue").InnerText, "Value was not updated.");
        }

    }
}
